<template>
	<view>
		<div>
			<navitop>
				<i class="iconfont" slot="left-slot" @click="outlot">&#xe667;</i>
				<span slot="center-slot">编辑详情</span>
				<!-- <span slot="right-slot"  @click="edit">保存</span> -->
			</navitop>
		</div>
		
		<div :style="'margin-top:' + nv_height"></div>
		
		<div class="public_list_style">
			<div class="public_list_out" v-for="(item,index) in results">
				<div class="public_list" v-if="item.onEdit == true">
					<div class="public_list_left">
					<text v-if="item.onAppFill == 1" style="color: red;">*</text>
					<text>{{item.name}}:</text>
					</div>
					<div class="public_list_right">
						<typeinput :type='item.type' :value='item.value' :values='item.val' :code="item.code" @user_alert="user_alert"></typeinput>
					</div>
				</div>
			</div>
		</div><br><br>
		<div :style="heigsdiv"></div>
		<div class="public_button" :style="heigs">
			<div class="public_text" @click="edit">
				保&nbsp;&nbsp;存
			</div>
		</div>
		
		<ztree ref='alert' @cityalerts="cityalerts" :dataSorce="citys_c"></ztree>
	</view>
</template>

<script>
	import navitop from '@/components/common/navi-top'
    import kxTime from "@/components/kx-datetime/kx-datetime"	
	import popus from '@/uni_modules/uni-popup/components/uni-popup/uni-popup'
	import ztree from '@/pages/common/common/city/z_citys.vue'
	import typeinput from '@/pages/common/common/public_input/type_input'
	import {detail,namelist,userdetail,distuser,city,list} from '@/common/api/job/jobapi.js'
	import {Z_threes} from '@/common/api/common/public_api.js'
	export default {
		components:{
			navitop,
			popus,
			kxTime,
			ztree,
			typeinput
		},
		data(){
			return {
				dataSorce: [],
				results: [],
				uid: '',
				sex:'',
				rmid: '',
				thistrue:'',
				tpid: '',
				date: '', //需要展示的时间数据
				enddate: "", // 时间段的终止时间，不设置默认当前之间终止
				startdate: "" ,//时间段的开始时间，不设置默认计算机最初时间
				citys_c: [],
				url: '',
				heigs: '',
				heigsdiv: 0,
				nv_height:''
			}
		},
		onLoad(option) {
			this.url =  '&crmType=' + option.crmType + '&name=' + option.name + '&classid=' + option.classid
			this.uid = option.id;
			this.tpid = option.uid;
			this.getpost();
			this.citys();
			uni.onKeyboardHeightChange(res => {				res.height = res.height + 50;
				this.heigsdiv = 'height:' + res.height  + 'px;' ;
				this.heigs = 'height:' + res.height + 'px;' ;
			})
	
		},
		mounted() {
			this.nv_height = uni.getSystemInfoSync().statusBarHeight + 75 + 'px';
		},
		methods:{
			outlot(){
				uni.navigateTo({
				    url: '/pages/common/index/contactedit?id=' + this.uid + '&uid=' + this.tpid + this.url 
				});
			},
			alertcity(){
				this.$refs.alert.z_three();
			},
			
			// 获取信息
			user_alert(val){
				this.results.forEach(item => {
					if(item.code == val.code){
						this.$set(item,'value',val.value)
					}
				})
			},
			
			// 获取地区
			cityalerts(val){
				for(let i in this.results){
					if(this.results[i].username == 'contactAddress'){
						this.results[i].value = val;
					}
				}
				console.log(this.results);
			},
			
			
			citys(){
				let children = [];
				city({current: 1,size: -1}).then((result)=> {
					this.citys_c = Z_threes(result.data,[])
				})
			},
			
			open() {
			     this.$refs.popup.open('right')
			},
			datetime(e){
				 this.date = e;
			},
			getpost(){
				var stringtable = [];
				var stringvalue = [];
				var sows = [];
				list({source: 'linkMan'}).then((row) => {
					if(row.code == 200){
						userdetail({id:this.uid}).then((result) => {
							this.rmid = result.data.crmId;
							if(result.code == 200){
								if(result.data.defineFields !== '' && result.data.defineFields !== null && result.data.defineFields !== undefined){
									result.data.defineFields = JSON.parse(result.data.defineFields);
								}
							
								row.data.forEach(item => {
									this.$set(item, 'value', result.data[item.code])
									if(result.data.defineFields !== '' && result.data.defineFields !== null && result.data.defineFields !== undefined && item.dataType == 2){
										this.$set(item, 'value', result.data.defineFields[item.id])
									}
								})
								this.results = row.data;
							}
						})
					}
				})
			},
			
			edit(){
				let uname = {};
				let define = {};
				let defineFields = '';
				for(let i in this.results){
					if(this.results[i].dataType == 1){
						uname[this.results[i].code] = this.results[i].value;
					}
					if( this.results[i].dataType == 2){
						define[this.results[i].id] = this.results[i].value
					}
				}
				uname['defineFields'] = JSON.stringify(define);
				uname['id'] = this.uid;
				uname['crmId'] = this.rmid;
				if(this.sex !== ''){
					uname['gender'] = this.sex;
				}
				console.log(uname)
				distuser(uname).then((rst) => {
					if(rst.code == 200){
						uni.showToast({
						    title: '修改成功！',
						});
						setTimeout(() => {
							uni.navigateTo({
							    url: '/pages/common/index/contactedit?id=' + this.uid + '&uid=' + this.tpid + this.url 
							});
						},1500)
					}else{
						uni.showToast({
						    title: '系统异常请联系管理员',
						});
					}
				})
			},
			
			radioChange(evt) {
				this.sex = evt.detail.value
			},
			radioChanges(val){
				this.thistrue = val.detail.value;
				console.log(val)
			}
		}
	}
	
</script>

<style scoped lang="scss">


</style>
